﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi.Task
{
    [CPPEI.WebApi.AuthGetAction("Task", "Get", "1.0")]
    public class GetAction : BaseAction<GetParams, GetResult>
    {
        protected override ActionResult<GetResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetParams opParams)
        {
            SER.TaskService service = new SER.TaskService();
            ServiceResult<CPPEI.ETL.Domain.Data.TaskDetail> serResult = service.GetTask(opParams.TaskId);
            if (serResult.IsSuccess)
            {
                GetResult item = new GetResult();
                item.Id = serResult.Data.Task.Id;
                item.Name = serResult.Data.Task.Name;
                item.Creator = serResult.Data.Task.Creator;
                item.CreatedTime = serResult.Data.Task.CreatedTime;
                item.DtSerId = serResult.Data.Task.DtSerId;
                item.DtSerText = serResult.Data.Task.DtSerName;
                item.DtSourceId = serResult.Data.Task.DtSourceId;
                item.DtSourceText = serResult.Data.Task.DtSourceName;

                if (!string.IsNullOrEmpty(serResult.Data.Task.DtTableName))
                {
                    item.DtTableText = serResult.Data.Task.DtTableName;
                }
                else
                {
                    item.DtTableText = string.Empty;
                }

                item.CompletedEvent = DefaultValue.GetValue(serResult.Data.Task.CompletedEvent, "");

                item.EnableCodeConversion = serResult.Data.Task.EnableCodeConversion;
                item.EnableDataVerification = serResult.Data.Task.EnableDataVerification;
                item.IsEnabled = serResult.Data.Task.IsEnabled;

                if (serResult.Data.Task.LastExecutedTime != null)
                {
                    item.LastExecutedTime = serResult.Data.Task.LastExecutedTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    item.LastExecutedTime = string.Empty;
                }

                item.RunMode = serResult.Data.Task.RunMode;

                #region runmodes
                for (int index = 0; index < serResult.Data.RunModes.Count; index++)
                {
                    GetResult.RunModeSettings settings = new GetResult.RunModeSettings();
                    settings.Mode = serResult.Data.RunModes[index].Mode;
                    if (serResult.Data.RunModes[index].AlarmClock == null)
                    {
                        settings.AlarmClock = "";
                    }
                    else
                    {
                        settings.AlarmClock = serResult.Data.RunModes[index].AlarmClock.Value.ToString("yyyy-MM-dd HH:mm");
                    }

                    if (serResult.Data.RunModes[index].AlarmMode == null)
                    {
                        settings.AlarmMode = 0;
                    }
                    else
                    {
                        settings.AlarmMode = serResult.Data.RunModes[index].AlarmMode.Value;
                    }

                    if (serResult.Data.RunModes[index].TimerIntervalMode == null)
                    {
                        settings.TimerIntervalMode = 0;
                    }
                    else
                    {
                        settings.TimerIntervalMode = serResult.Data.RunModes[index].TimerIntervalMode.Value;
                    }

                    if (serResult.Data.RunModes[index].TimerInterval == null)
                    {
                        settings.TimerInterval = 0;
                    }
                    else
                    {
                        settings.TimerInterval = serResult.Data.RunModes[index].TimerInterval.Value;
                    }


                    if (serResult.Data.RunModes[index].TimerBeginTime == null)
                    {
                        settings.TimerBeginTime = "";
                    }
                    else
                    {
                        settings.TimerBeginTime = serResult.Data.RunModes[index].TimerBeginTime.Value.ToString("yyyy-MM-dd HH:mm");
                    }

                    if (string.IsNullOrEmpty(serResult.Data.RunModes[index].EventName))
                    {
                        settings.EventName = "";
                    }
                    else
                    {
                        settings.EventName = serResult.Data.RunModes[index].EventName;
                    }

                    item.RunModes.Add(settings);
                }
                #endregion
                for (int index = 0; index < serResult.Data.Parameters.Count; index++)
                {
                    GetResult.Patameter parameter = new GetResult.Patameter();
                    parameter.Name = serResult.Data.Parameters[index].Name;
                    parameter.ValueType = serResult.Data.Parameters[index].ValueType;
                    parameter.ValueString = serResult.Data.Parameters[index].ValueString;
                    item.Patameters.Add(parameter);
                }

                return new ActionResult<GetResult>(item);
            }
            else
            {
                return new ActionResult<GetResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetParams
    {
        /// <summary>
        /// 任务id
        /// </summary>
        public Guid TaskId { get; set; }
    }

    public class GetResult
    {
        public Guid Id { get; set; }

        public string Name { get; set; }

        public Guid DtSerId { get; set; }

        public Guid DtSourceId { get; set; }

        public bool IsDeleted { get; set; }

        public bool IsEnabled { get; set; }

        public string Creator { get; set; }

        public DateTime CreatedTime { get; set; }

        public int RunMode { get; set; }

        public bool EnableDataVerification { get; set; }

        public bool EnableCodeConversion { get; set; }

        public string CompletedEvent { get; set; }

        public string LastExecutedTime { get; set; }

        public string DtSourceText { get; set; }

        public string DtSerText { get; set; }

        public string DtTableText { get; set; }

        public List<RunModeSettings> RunModes = new List<RunModeSettings>();
        public List<Patameter> Patameters = new List<Patameter>();

        public class RunModeSettings
        {
            public int Mode { get; set; }

            public string TimerBeginTime { get; set; }
            /// <summary>
            /// Timer,Interval
            /// </summary>
            public int TimerInterval { get; set; }

            /// <summary>
            /// Day,Minute,Second
            /// </summary>
            public int TimerIntervalMode { get; set; }

            /// <summary>
            /// alarm, clockmode:PY,PM,PW,PD
            /// </summary>
            public int AlarmMode { get; set; }

            /// <summary>
            /// Alarm,Clock
            /// </summary>
            public string AlarmClock { get; set; }

            /// <summary>
            /// Event,Event
            /// </summary>
            public string EventName { get; set; }
        }

        public class Patameter
        {
            public string Name { get; set; }
            public int ValueType { get; set; }
            public string ValueString { get; set; }
        }
    }
}
